package org.liangsy.common.bean;


import java.io.Serializable;

public class Result<T> implements Serializable {
    private T result;
    private String code;
    private String msg;
    private String serial;


    public Result(String code, String msg, T result) {
        this.code = code;
        this.msg =msg;
        this.result = result;
    }

    public Result() {

    }

    public boolean isOk() {
        // 判断当前的 code 是否等于成功的状态码
        return ResultCode.SUCCESS.getResultCode().equals(this.code);
    }

    public static <T> Result<T> success() {
        Result result = new Result();
        result.setCode(ResultCode.SUCCESS.getResultCode());
        result.setMsg(ResultCode.SUCCESS.getResultMsg());
        result.setResult((T) null);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result();
        result.setCode(ResultCode.SUCCESS.getResultCode());
        result.setMsg(ResultCode.SUCCESS.getResultMsg());
        result.setResult(data);
        return result;
    }

    public static <T> Result<T> failed(String code, String msg) {
        Result<T> result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setResult((T) null);
        return result;
    }

    public static <T> Result<T> failed(String msg) {
        Result<T> result = new Result();
        result.setCode(ResultCode.FAILED.getResultCode());
        result.setMsg(msg);
        result.setResult((T) null);
        return result;
    }

    public static <T> Result<T> failed(ResultCode errorCode) {
        Result<T> result = new Result();
        result.setCode(errorCode.getResultCode());
        result.setMsg(errorCode.getResultMsg());
        result.setResult((T)null);
        return result;
    }
    public T getResult() {
        return this.result;
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public Result<T> setResult(final T result) {
        this.result = result;
        return this;
    }

    public Result<T> setCode(final String code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(final String msg) {
        this.msg = msg;
        return this;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$result = this.getResult();
                    Object other$result = other.getResult();
                    if (this$result == null) {
                        if (other$result == null) {
                            break label47;
                        }
                    } else if (this$result.equals(other$result)) {
                        break label47;
                    }

                    return false;
                }

                Object this$code = this.getCode();
                Object other$code = other.getCode();
                if (this$code == null) {
                    if (other$code != null) {
                        return false;
                    }
                } else if (!this$code.equals(other$code)) {
                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Result;
    }

    public int hashCode() {
        int result = 1;
        Object $result = this.getResult();
        result = result * 59 + ($result == null ? 43 : $result.hashCode());
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        return result;
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }
}
